package net.hn.hnms.biz.energy.service.impl;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.energy.domain.PowerAnalysis;
import net.hn.hnms.biz.energy.mapper.PowerAnalysisMapper;
import net.hn.hnms.biz.energy.request.PowerAnalysisReq;
import net.hn.hnms.biz.energy.service.PowerAnalysisService;
import net.hn.hnms.biz.energy.vo.PowerStatisticsInfoVo;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.UtilException;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class PowerAnalysisServiceImpl extends BaseService<PowerAnalysisReq, PowerAnalysis> implements PowerAnalysisService {

    @Autowired
    private PowerAnalysisMapper powerAnalysisMapper;

    @Override
    public List<PowerStatisticsInfoVo> calculationPower() {
        //每天定时查询截至到今天的前一天的数据加到当前月中
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date d = cal.getTime();
        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
        int year = cal.get(Calendar.YEAR);
        String ZUOTIAN = sp.format(d);
        DynamicDataSourceContextHolder.push("power");
        List<PowerStatisticsInfoVo> powerStatisticsInfoVos = powerAnalysisMapper.calculationPower(year,ZUOTIAN);
//        List<PowerStatisticsInfoVo> powerStatisticsInfoVos = new ArrayList<>();
//        PowerStatisticsInfoVo v = new PowerStatisticsInfoVo();
//        v.setId("1");
//        v.setTrough(new BigDecimal(1));
//        v.setAverage(new BigDecimal(1));
//        v.setHighest(new BigDecimal(1));
//        v.setTroughOneFive(new BigDecimal(1));
//        v.setTroughElevenSeventeen(new BigDecimal(1));
//        v.setAverageFiveSeven(new BigDecimal(1));
//        v.setAverageNineEleven(new BigDecimal(1));
//        v.setAverageSeventeenEighteen(new BigDecimal(1));
//        v.setAverageZeroOne(new BigDecimal(1));
//        v.setHighestSevenNine(new BigDecimal(1));
//        v.setHighestEighteenTwefour(new BigDecimal(1));
//        v.setYears("2022-01");
////        powerStatisticsInfoVos.add(v);
//        PowerStatisticsInfoVo v1 = new PowerStatisticsInfoVo();
////        v1.setId("1");
//        v1.setTrough(new BigDecimal(1));
//        v1.setAverage(new BigDecimal(1));
//        v1.setHighest(new BigDecimal(1));
//        v1.setTroughOneFive(new BigDecimal(1));
//        v1.setTroughElevenSeventeen(new BigDecimal(1));
//        v1.setAverageFiveSeven(new BigDecimal(1));
//        v1.setAverageNineEleven(new BigDecimal(1));
//        v1.setAverageSeventeenEighteen(new BigDecimal(1));
//        v1.setAverageZeroOne(new BigDecimal(1));
//        v1.setHighestSevenNine(new BigDecimal(1));
//        v1.setHighestEighteenTwefour(new BigDecimal(1));
//        v1.setYears("2023-05");
//        powerStatisticsInfoVos.add(v1);
        DynamicDataSourceContextHolder.clear();
        if (CollectionUtils.isNotEmpty(powerStatisticsInfoVos)) {
            for (PowerStatisticsInfoVo powerStatisticsInfoVo : powerStatisticsInfoVos) {
                if (powerStatisticsInfoVo != null) {
                    if (powerStatisticsInfoVo.getTrough()!=null&&powerStatisticsInfoVo.getTrough().compareTo(new BigDecimal(0))==1) {
                        //根据年月查询数据库并且相加
                        PowerStatisticsInfoVo vo = powerAnalysisMapper.selectPowerConsumptionAnalysisByYearOfMonth(powerStatisticsInfoVo.getYears(), null);
                        if (vo != null) {
                            //修改
//                            PowerAnalysis powerAnalysis = new PowerAnalysis();
//                            vo.setTrough(vo.getTrough()+powerStatisticsInfoVo.getTrough());
//                            vo.setAverage(vo.getAverage()+powerStatisticsInfoVo.getAverage());
//                            vo.setHighest(vo.getHighest()+powerStatisticsInfoVo.getHighest());
//                            vo.setTroughOneFive(vo.getTroughOneFive()+powerStatisticsInfoVo.getTroughOneFive());
//                            vo.setTroughElevenSeventeen(vo.getTroughElevenSeventeen()+powerStatisticsInfoVo.getTroughElevenSeventeen());
//                            vo.setAverageFiveSeven(vo.getAverageFiveSeven()+powerStatisticsInfoVo.getAverageFiveSeven());
//                            vo.setAverageNineEleven(vo.getAverageNineEleven()+powerStatisticsInfoVo.getAverageNineEleven());
//                            vo.setAverageSeventeenEighteen(vo.getAverageSeventeenEighteen()+powerStatisticsInfoVo.getAverageSeventeenEighteen());
//                            vo.setAverageZeroOne(vo.getAverageZeroOne()+powerStatisticsInfoVo.getAverageZeroOne());
//                            vo.setHighestSevenNine(vo.getHighestSevenNine()+powerStatisticsInfoVo.getHighestSevenNine());
//                            vo.setHighestEighteenTwefour(vo.getHighestEighteenTwefour()+powerStatisticsInfoVo.getHighestEighteenTwefour());
//                            BeanUtil.copyProperties(powerStatisticsInfoVo, powerAnalysis);
//                            powerAnalysisMapper.updateById(powerAnalysis);
                            BigDecimal trough = vo.getTrough().add(powerStatisticsInfoVo.getTrough());

                            BigDecimal average = vo.getAverage().add(powerStatisticsInfoVo.getAverage());
                            BigDecimal hh =  powerStatisticsInfoVo.getHighest()==null?new BigDecimal(0): powerStatisticsInfoVo.getHighest();
                            BigDecimal highest = vo.getHighest().add(hh);
                            BigDecimal troughOneFive = vo.getTroughOneFive().add(powerStatisticsInfoVo.getTroughOneFive());
                            BigDecimal troughElevenSeventeen = vo.getTroughElevenSeventeen().add(powerStatisticsInfoVo.getTroughElevenSeventeen());
                            BigDecimal averageZeroOne = vo.getAverageZeroOne().add(powerStatisticsInfoVo.getAverageZeroOne());
                            BigDecimal averageFiveSeven = vo.getAverageFiveSeven().add(powerStatisticsInfoVo.getAverageFiveSeven());
                            BigDecimal averageNineEleven = vo.getAverageNineEleven().add(powerStatisticsInfoVo.getAverageNineEleven());
                            BigDecimal averageSeventeenEighteen = vo.getAverageSeventeenEighteen().add(powerStatisticsInfoVo.getAverageSeventeenEighteen());
                            BigDecimal highestSevenNine = vo.getHighestSevenNine().add(powerStatisticsInfoVo.getHighestSevenNine());
                            BigDecimal hetf =  powerStatisticsInfoVo.getHighestEighteenTwefour()==null?new BigDecimal(0): powerStatisticsInfoVo.getHighestEighteenTwefour();
                            BigDecimal highestEighteenTwefour = vo.getHighestEighteenTwefour().add(hetf);
                            int i = powerAnalysisMapper.updatePowerAnalysisByIds(
                                    vo.getYears(),
                                    trough.toString(),
                                    average.toString(),
                                    highest.toString(),
                                    troughOneFive.toString(),
                                    troughElevenSeventeen.toString(),
                                    averageZeroOne.toString(),
                                    averageFiveSeven.toString(),
                                    averageNineEleven.toString(),
                                    averageSeventeenEighteen.toString(),
                                    highestSevenNine.toString(),
                                    highestEighteenTwefour.toString()
                                    );
                        } else {
                            //插入
                            PowerAnalysis insertObj = new PowerAnalysis();
                            insertObj.setYears(powerStatisticsInfoVo.getYears());
                            insertObj.setAverage(powerStatisticsInfoVo.getAverage().toString());
                            insertObj.setTrough(powerStatisticsInfoVo.getTrough().toString()==null?"0":powerStatisticsInfoVo.getTrough().toString());
                            insertObj.setHighest(powerStatisticsInfoVo.getHighest().toString());
                            insertObj.setTroughOneFive(powerStatisticsInfoVo.getTroughOneFive().toString());
                            insertObj.setTroughElevenSeventeen(powerStatisticsInfoVo.getTroughElevenSeventeen().toString());
                            insertObj.setAverageFiveSeven(powerStatisticsInfoVo.getAverageFiveSeven().toString());
                            insertObj.setAverageNineEleven(powerStatisticsInfoVo.getAverageNineEleven().toString());
                            insertObj.setAverageSeventeenEighteen(powerStatisticsInfoVo.getAverageSeventeenEighteen().toString());
                            insertObj.setAverageZeroOne(powerStatisticsInfoVo.getAverageZeroOne().toString());
                            insertObj.setHighestSevenNine(powerStatisticsInfoVo.getHighestSevenNine().toString());
                            insertObj.setHighestEighteenTwefour(powerStatisticsInfoVo.getHighestEighteenTwefour().toString()==null?"0":powerStatisticsInfoVo.getHighestEighteenTwefour().toString());
                            powerAnalysisMapper.insert(insertObj);
                        }
                    }
                }
            }
        }
        return null;
    }

    @Override
    public List<PowerStatisticsInfoVo> selectPowerConsumptionAnalysisByYear(String year, String mineCode) {
        if (StringUtils.isBlank(year)) {
            throw new UtilException("参数不符合规范，不能进行查询");
        }
//        DynamicDataSourceContextHolder.push("power");
        List<PowerStatisticsInfoVo> infoVos = powerAnalysisMapper.selectPowerConsumptionAnalysisByYear(year, mineCode);
//        DynamicDataSourceContextHolder.clear();
        return infoVos;
    }


    @Override
    public ResponsePage<PowerStatisticsInfoVo> selectPowerConsumptionAnalysisDetails(PageQuery pageQuery, String beginDate, Integer flag, String mineCode) {
        if (StringUtils.isBlank(beginDate)) {
            throw new UtilException("参数不符合规范，不能进行查询");
        }
        if (flag >= 4 || flag <= 0) {
            throw new UtilException("参数不符合规范，不能进行查询");
        }
//        DynamicDataSourceContextHolder.push("power");
        Page<PowerStatisticsInfoVo> page = powerAnalysisMapper.selectPowerConsumptionAnalysisDetails(pageQuery.build(), beginDate, flag, mineCode);
//        DynamicDataSourceContextHolder.clear();
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    @Override
    public PowerStatisticsInfoVo selectPowerConsumptionAnalysisByYearOfMonth(String yearOfMonth, String mineCode) {
        powerAnalysisMapper.selectPowerConsumptionAnalysisByYearOfMonth(yearOfMonth, mineCode);
        return null;
    }


}
